package com.siki.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.siki.dao.UserMapper;
import com.siki.pojo.model.User;
import com.siki.utils.WebUtils;
import com.siki.config.jsonConfig.JsonResult;
import com.siki.vo.SignUpUserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

import static com.siki.utils.WebUtils.*;

/**
 * @Author Siki
 * @Date 2020/4/23
 */
@RestController
@Api(tags = "url - 身份验证相关接口")
@RequestMapping("/getRole")
@CrossOrigin
public class UrlController {

    private final static String cookie_Domain="dtwu.club";

    private final UserMapper userMapper;

    public UrlController(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    /**
     * 只是负责验证是否登陆，所以就没有用三层架构的方式
     */
    @RequestMapping(value = "/verification", method = RequestMethod.GET)
    @ApiOperation(value = "[验证接口] - 中央认证",consumes = "application/json",httpMethod = "GET")
    public JsonResult verification(HttpServletResponse httpServletResponse, @RequestParam("token") String token) throws IOException {
        if (Strings.isEmpty(token)) {
            httpServletResponse.sendRedirect(CAS_URL);
        }
            JSONObject request_Token = new JSONObject();
            request_Token.put("token", token);
            JSONObject http_Response_Result_Json = WebUtils.sendPostWithJson(Verification_User_URL, request_Token);
            Integer error_code = (Integer) http_Response_Result_Json.get("error_code");
            if(null==error_code){       //如果验证结果正常,不会有error_code;
                http_Response_Result_Json.put("token",token);
                Cookie cookie = new Cookie("token", token);
                cookie.setDomain(cookie_Domain);
                cookie.setPath("/");
                cookie.setHttpOnly(false);
                cookie.setMaxAge(180000);
                httpServletResponse.addCookie(cookie);
                httpServletResponse.sendRedirect(DTWU_JUMP_URL+"?token="+token);
                return JsonResult.success();
            }else {
                return JsonResult.fail(http_Response_Result_Json.get("msg").toString());
            }
    }

    @RequestMapping(value = "/returnInfo", method = RequestMethod.GET)
    @ApiOperation(value = "[查询接口] - 中央认证返回信息",consumes = "application/json",httpMethod = "GET")
    public JsonResult returnInfo(@ApiParam("token") @RequestParam("token") String token) {
            JSONObject request_Token = new JSONObject();
            request_Token.put("token", token);
            JSONObject http_Response_Result_Json = WebUtils.sendPostWithJson(Verification_User_URL, request_Token);
            Integer error_code = (Integer) http_Response_Result_Json.get("error_code");
            if(null==error_code){       //如果验证结果正常,不会有error_code;
                QueryWrapper<User> selectWrapper = new QueryWrapper<>();
                selectWrapper.eq("number",http_Response_Result_Json.get("studentNumber"));
                User user = userMapper.selectOne(selectWrapper);
                if(Objects.nonNull(user)){
                    SignUpUserInfoVO sign_up_user_info = new SignUpUserInfoVO(user);
                    System.out.println(LocalDateTime.now().
                            format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))+" :(修改)"+sign_up_user_info.getName());
                    return JsonResult.success(sign_up_user_info);
                }
                System.out.println(LocalDateTime.now().
                        format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))+" : "+http_Response_Result_Json);
                http_Response_Result_Json.put("token",token);
                return JsonResult.success(http_Response_Result_Json);
            }else {
                return JsonResult.fail(http_Response_Result_Json.get("msg").toString());
            }
    }
}
